import axios from "axios";
import { getCookie } from "./auth";

const instance = axios.create({
    baseURL: "https://sapi.jiangdaye.cn/",
    timeout: 10000,
});
// http://192.168.16.43:9999/
//请求拦截器
instance.interceptors.request.use(
    function (config) {
        if (getCookie) {
            config.headers["Authorization"] = `Bearer ` + getCookie();
        }
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

//响应拦截器
instance.interceptors.response.use(
    function (response) {
        return response.data;
    },
    function (error) {
        if (error.response.status === 401) {
            window.location.href = '/login';
            return;
        }
        return Promise.reject(error);
    }
);

export default instance;